<template>
  <el-card class="box-card">
    <div class="clearfix">
      <!-- tabs栏 -->
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="销售额" name="sale"></el-tab-pane>
        <el-tab-pane label="访问量" name="visite"></el-tab-pane>
      </el-tabs>
      <!-- 右侧内容 -->
      <div class="right">
        <span @click="setDay('1')">今日</span>
        <span @click="setDay('2')">本周</span>
        <span @click="setDay('3')">本月</span>
        <span @click="setDay('4')">本年</span>
        <el-date-picker
          v-model="date"
          value-format="yyyy-MM-dd"
          type="daterange"
          align="right"
          unlink-panels
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          size="mini"
          style="margin: 0px 10px"
        >
        </el-date-picker>
      </div>
    </div>
    <!--内容区域 -->
    <div class="center">
      <el-row :gutter="10">
        <el-col :xs="16" :sm="18" :md="18" :lg="16" :xl="18">
          <div class="charts" ref="charts"></div>
        </el-col>
        <el-col :xs="8" :sm="6" :md="6" :lg="8" :xl="6">
          <div class="pm">
            <h3>门店{{ title }}排名</h3>
            <ul>
              <li>
                <span class="rindex">1</span>
                <span class="rname">肯德基</span>
                <span class="rnum">999</span>
              </li>
              <li>
                <span class="rindex">2</span>
                <span class="rname">肯德基</span>
                <span class="rnum">999</span>
              </li>
              <li>
                <span class="rindex">3</span>
                <span class="rname">肯德基</span>
                <span class="rnum">999</span>
              </li>
              <li>
                <span class="rindex1">4</span>
                <span class="rname">肯德基</span>
                <span class="rnum">999</span>
              </li>
              <li>
                <span class="rindex1">5</span>
                <span class="rname">肯德基</span>
                <span class="rnum">999</span>
              </li>
              <li>
                <span class="rindex1">6</span>
                <span class="rname">肯德基</span>
                <span class="rnum">999</span>
              </li>
              <li>
                <span class="rindex1">7</span>
                <span class="rname">肯德基</span>
                <span class="rnum">999</span>
              </li>
            </ul>
          </div>
        </el-col>
      </el-row>
    </div>
  </el-card>
</template>

<script>
// 引入dayjs
import dayjs from "dayjs";
export default {
  name: "Sale",
  data() {
    return {
      activeName: "sale",
      mycharts: null,
      date: [],
    };
  },
  mounted() {
    let echarts = require("echarts");
    // console.log(echarts);
    // 将echarts挂载到组件实例上便于修改配置项
    this.mycharts = echarts.init(this.$refs.charts);
    this.mycharts.setOption({
      title: {
        text: `${this.title}趋势`,
      },
      tooltip: {
        trigger: "axis",
        axisPointer: {
          type: "shadow",
        },
      },
      grid: {
        left: "3%",
        right: "4%",
        bottom: "3%",
        containLabel: true,
      },
      xAxis: [
        {
          type: "category",
          data: [
            "一月",
            "二月",
            "三月",
            "四月",
            "五月",
            "六月",
            "七月",
            "八月",
            "九月",
            "十月",
            "十一月",
            "十二月",
          ],
          axisTick: {
            alignWithLabel: true,
          },
        },
      ],
      yAxis: [
        {
          type: "value",
        },
      ],
      series: [
        {
          name: "Direct",
          type: "bar",
          barWidth: "60%",
          data: [10, 52, 200, 334, 390, 330, 120, 270, 170, 390, 360, 250],
        },
      ],
    });
  },
  methods: {
    // 点击切换tabs栏触发的方法
    handleClick(tab, event) {
      console.log(tab, event);
    },
    // 今日
    setDay(value) {
      //今日
      if (value == "1") {
        // 获取当前时间并转换格式
        const thisDay = dayjs().format("YYYY-MM-DD");
        this.date = [thisDay, thisDay];
      }
      // 本周
      if(value == '2'){
        const start = dayjs().day(1).format('YYYY-MM-DD')
        const end  = dayjs().day(7).format('YYYY-MM-DD')
        this.date = [start, end];
      }
      // 本月
      if(value == '3'){
        const start = dayjs().startOf('month').format('YYYY-MM-DD')
        const end  = dayjs().endOf('month').format('YYYY-MM-DD')
        this.date = [start, end];
      }
      // 本年
      if(value == '4'){
        const start = dayjs().startOf('year').format('YYYY-MM-DD')
        const end  = dayjs().endOf('year').format('YYYY-MM-DD')
        this.date = [start, end];
      }
    },
  },

  computed: {
    title() {
      return this.activeName == "sale" ? "销售额" : "访问量";
    },
  },
  watch: {
    // echarts是放在mounted里面的只会执行一次，因此在title发生变化时我没要重新修改配置项
    title() {
      this.mycharts.setOption({
        title: {
          text: `${this.title}趋势`,
        },
      });
    },
  },
};
</script>

<style scoped>
.clearfix {
  position: relative;
  display: flex;
  justify-content: space-between;
}
.right {
  margin: 10px 20px;
  position: absolute;
  right: 0;
}
.charts {
  height: 500px;
  width: 95%;
}
.right span {
  margin: 0px 5px;
}
.pm h3 {
  margin: 0 0 20px 0;
}
.pm ul {
  height: 450px;
  width: 100%;
  padding: 0;
}
.pm ul li {
  height: 8%;
  width: 100%;
  margin: 10px 0;
  list-style: none;
}
.rindex {
  width: 100px;
  height: 20px;
  border-radius: 50%;
  background-color: black;
  color: white;
  text-align: center;
  padding: 3px 7px;
}
.rindex1 {
  width: 100px;
  height: 20px;
  text-align: center;
  padding: 3px 7px;
}
.rname {
  margin-left: 20px;
}
.rnum {
  float: right;
  margin: 0 10px;
}
</style>